﻿@{
    Layout = "";

    var gridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSize;
}
@model Nop.Plugin.Tax.CountryStateZip.Models.TaxRateListModel
@using Nop.Web.Framework;
@using Telerik.Web.Mvc.UI;
@using Nop.Plugin.Tax.CountryStateZip.Models;
@using Nop.Core.Infrastructure;
<table class="adminContent">
    <tr>
        <td>
            @(Html.Telerik().Grid<TaxRateModel>()
                    .Name("tax-countrystatezip-grid")
                    .DataKeys(keys => keys.Add(x => x.Id).RouteKey("Id"))
                    .Columns(columns =>
                    {
                        columns.Bound(x => x.StoreName).ReadOnly();
                        columns.Bound(x => x.CountryName).ReadOnly();
                        columns.Bound(x => x.StateProvinceName).ReadOnly();
                        columns.Bound(x => x.Zip);
                        columns.Bound(x => x.TaxCategoryName).ReadOnly();
                        columns.Bound(x => x.Percentage).Format("{0:G29}");
                        columns.Command(commands =>
                        {
                            commands.Edit().Text(T("Admin.Common.Edit").Text);
                            commands.Delete().Text(T("Admin.Common.Delete").Text);
                        }).Width(180);

                    })
                    .Editable(x =>
                    {
                        x.Mode(GridEditMode.InLine);
                    })
                    .DataBinding(dataBinding =>
                    {
                        dataBinding.Ajax()
                            .Select("RatesList", "TaxCountryStateZip", new RouteValueDictionary() { { "Namespaces", "Nop.Plugin.Tax.CountryStateZip.Controllers" }, { "area", "" } })
                            .Update("RateUpdate", "TaxCountryStateZip", new RouteValueDictionary() { { "Namespaces", "Nop.Plugin.Tax.CountryStateZip.Controllers" }, { "area", "" } })
                            .Delete("RateDelete", "TaxCountryStateZip", new RouteValueDictionary() { { "Namespaces", "Nop.Plugin.Tax.CountryStateZip.Controllers" }, { "area", "" } });
                    })
                    .Pageable(settings => settings.PageSize(gridPageSize).Position(GridPagerPosition.Both))
                    .EnableCustomBinding(true))
        </td>
    </tr>
</table>
<p>
</p>
<script type="text/javascript">
     $(function() {
            $("#@Html.FieldIdFor(model => model.AddCountryId)").change(function() {
                var selectedItem = $(this).val();
                var ddlStates = $("#@Html.FieldIdFor(model => model.AddStateProvinceId)");
                $.ajax({
                    cache:false,
                    type: "GET",
                    url: "@(Url.Action("GetStatesByCountryId", "Country", new RouteValueDictionary() { { "area", "Admin" } }))",
                    data: { "countryId": selectedItem, "addAsterisk": "true" },
                    success: function (data) {
                        ddlStates.html('');
                        $.each(data, function(id, option) {
                            ddlStates.append($('<option></option>').val(option.id).html(option.name));
                        });
                    },
                    error:function (xhr, ajaxOptions, thrownError){
                        alert('Failed to retrieve states.');
                    }  
                });
            });
        });
</script>
@using (Html.BeginForm())
{
    <table class="adminContent">
        <tr>
            <td class="adminTitle" colspan="2">
                <strong>@T("Plugins.Tax.CountryStateZip.AddRecord.Hint")</strong>
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.AddStoreId):
            </td>
            <td class="adminData">
                @Html.DropDownListFor(model => model.AddStoreId, Model.AvailableStores)
                @Html.ValidationMessageFor(model => model.AddStoreId)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.AddCountryId):
            </td>
            <td class="adminData">
                @Html.DropDownListFor(model => model.AddCountryId, Model.AvailableCountries)
                @Html.ValidationMessageFor(model => model.AddCountryId)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.AddStateProvinceId):
            </td>
            <td class="adminData">
                @Html.DropDownListFor(model => model.AddStateProvinceId, Model.AvailableStates)
                @Html.ValidationMessageFor(model => model.AddStateProvinceId)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.AddZip):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.AddZip)
                @Html.ValidationMessageFor(model => model.AddZip)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.AddTaxCategoryId):
            </td>
            <td class="adminData">
                @Html.DropDownListFor(model => model.AddTaxCategoryId, Model.AvailableTaxCategories)
                @Html.ValidationMessageFor(model => model.AddTaxCategoryId)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.AddPercentage):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.AddPercentage)
                @Html.ValidationMessageFor(model => model.AddPercentage)
            </td>
        </tr>
        <tr>
            <td colspan="2" align="left">
                <input type="button" id="addtaxrate" class="t-button" value="@T("Plugins.Tax.CountryStateZip.AddRecord")" />
            </td>
        </tr>
    </table>
    <script type="text/javascript">
        $(function () {
            $('#addtaxrate').click(function () {
                $.ajax({
                    cache: false,
                    type: 'POST',
                    url: '@Url.RouteUrl("Plugin.Tax.CountryStateZip.AddTaxRate")',
                    data: $(this.form).serialize(),
                    dataType: 'json',
                    success: function (data) {
                        var taxCountrystatezipGrid = $("#tax-countrystatezip-grid");
                        taxCountrystatezipGrid.data('tGrid').ajaxRequest();
                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                        alert('Failed to add record.');
                    }
                });
                return false;
            });
        });
    </script>
}